package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * @Description:
 * @author: JC
 * @date: 2021/11/2 8:48
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(UserInfo userInfo) {

        String userInfoName = userInfo.getLoginName();
        String userInfoPwdForm = userInfo.getPasswd();
        // MD5加密
        userInfoPwdForm = DigestUtils.md5DigestAsHex(userInfoPwdForm.getBytes());

        // 数据库查出来到数据
        UserInfo userInfoDB = userInfoMapper.selectOne(new QueryWrapper<UserInfo>().eq("login_name", userInfoName));

        if(userInfoDB == null){
            return null;
            // throw new GmallException("用户名或密码错误",123);
        }
        // 数据库中密码
        String userInfoPwdDB = userInfoDB.getPasswd();

        if(!StringUtils.equals(userInfoPwdForm,userInfoPwdDB)){
            return null;
            // throw new GmallException("用户名或密码错误",123);
        }

        return userInfoDB;
    }
}
